package encaps.utils;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractCache<K, V> {

    private Map<K, V> cachedObjects;

    public AbstractCache() {
        cachedObjects = new HashMap<K, V>();
    }

    public V get(K key) throws DataAccessException {
        if (!isObjectLoadel(key)) {
            loadObjectToCache(key);
        }
        return cachedObjects.get(key);
    }

    public void reset() {
        cachedObjects.clear();
    }

    protected void putIntoCache(K key, V value) {
        cachedObjects.put(key, value);
    }

    protected boolean isObjectLoadel(K key) {
        return cachedObjects.containsKey(key);
    }

    protected abstract void loadObjectToCache(K key) throws DataAccessException;
}
